<template>
  <div class="contents">
    <div class="top">
      <div class="tabs">
        <span :class="{'checked': transType === 1}" @click="changeTransType(1)">
          买入订单
          <p class="badge" v-if="countObj.buyCount.sum>0">
            {{countObj.buyCount.sum}}
          </p>
        </span>
        <span :class="{'checked': transType === 2}" @click="changeTransType(2)">
          卖出订单
          <p class="badge" v-if="countObj.saleCount.sum>0">
            {{countObj.saleCount.sum}}
          </p>
        </span>
      </div>
    </div>

    <van-tabs class="sort">
      <van-tab title="数量">
        <template #title>
          <div :class="{blueText: status===''}" @click="changeStatus('')">
            <span style="position: relative;">
              全部
              <p class="badge2" v-if="transType === 1&&countObj.buyCount.sum>0">
                {{countObj.buyCount.sum}}
              </p>
              <p class="badge2" v-if="transType === 2&&countObj.saleCount.sum>0">
                {{countObj.saleCount.sum}}
              </p>
            </span>
          </div>
        </template>
      </van-tab>
      <van-tab>
        <template #title>
          <div :class="{blueText: status===1}" @click="changeStatus(1)">
            <span style="position: relative;">
              待确认
              <p class="badge2" v-if="transType === 1&&countObj.buyCount.confirmCount>0">
                {{countObj.buyCount.confirmCount}}
              </p>
              <p class="badge2" v-if="transType === 2&&countObj.saleCount.confirmCount>0">
                {{countObj.saleCount.confirmCount}}
              </p>
            </span>
          </div>
        </template>
      </van-tab>
      <van-tab>
        <template #title>
          <div :class="{blueText: status===2}" @click="changeStatus(2)">
            <span style="position: relative;">
              待付款
              <p class="badge2" v-if="transType === 1&&countObj.buyCount.payCount>0">
                {{countObj.buyCount.payCount}}
              </p>
              <p class="badge2" v-if="transType === 2&&countObj.saleCount.payCount>0">
                {{countObj.saleCount.payCount}}
              </p>
            </span>
          </div>
        </template>
      </van-tab>
      <van-tab>
        <template #title>
          <div :class="{blueText: status===3}" @click="changeStatus(3)">
            <span style="position: relative;">
              待发货
              <p class="badge2" v-if="transType === 1&&countObj.buyCount.sendCount>0">
                {{countObj.buyCount.sendCount}}
              </p>
              <p class="badge2" v-if="transType === 2&&countObj.saleCount.sendCount>0">
                {{countObj.saleCount.sendCount}}
              </p>
            </span>
          </div>
        </template>
      </van-tab>
      <van-tab>
        <template #title>
          <div :class="{blueText: status===14}" @click="changeStatus(4)">
            已完成
          </div>
        </template>
      </van-tab>
    </van-tabs>


    <div class="content" :style="{height: 'calc(100% - 115px)'}">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list style="height: 100%;overflow: auto;" v-model="loading" :finished="finished" finished-text="没有更多了"
          @load="onLoad">
          <template v-for="(item,index) in list">
            <div v-if="item.receivePayment.length!=0" class="card" :key="index">
              <div class="info">
                <img class="headImg" :src="baseData&&baseData.data.imgUrl + item.avatar" />
                <span>{{item.nickName}}</span>
                <img class="creditImg" v-if="item.grade"
                  :src="require(`@/assets/images/creditType/${item.grade}.png`)" />
              </div>
              <div class="line1">
                <span class="t1">卖出</span>
                <span class="t2">{{item.transCount.toFixed(2)}} B</span>
              </div>
              <div class="line2">
                <span class="t1">单价</span>
                <span class="t2"> ¥ {{item.transPrice.toFixed(2)}}</span>
              </div>
              <div class="line3">
                <img :src="require(`@/assets/images/receivePayment/${xitem}.png`)"
                  v-for="(xitem,xindex) in item.receivePayment" :key="xindex" />
              </div>
              <div class="right">
                <div class="line1" :style="{color: getTradeType(item.transStatus).color}">
                  <span class="t5">{{getTradeType(item.transStatus).label}}</span>
                  <van-icon name="arrow" />
                </div>
                <div class="line2">
                  <span class="t1">￥ {{item.transAmount.toFixed(2)}}</span>
                </div>
                <div v-if="item.transStatus === 10007" class="btn" @click="goEvaluation(item)">去评价</div>
              </div>
            </div>
          </template>
        </van-list>
      </van-pull-refresh>
    </div>

    <div v-if="transType!==1" class="bigBtn">
      <div>出售</div>
    </div>
  </div>
</template>

<script>
  import api from "@/api/trade";
  import {
    mapState,
    mapActions
  } from "vuex";
  import indexOf from "core-js/fn/array/index-of";
  let that = {
    source: null
  }
  export default {
    data() {
      return {
        statusList: [{
            value: '10001',
            label: '出售中',
            color: 'gray'
          },
          {
            value: '10002',
            label: '暂停出售',
            color: 'red'
          },
          {
            value: '10003',
            label: '等待卖家确认',
            color: 'gray'
          },
          {
            value: '10004',
            label: '交易中',
            color: 'gray'
          },
          {
            value: '10005',
            label: '卖家取消',
            color: 'red'
          },
          {
            value: '10014',
            label: '买家取消',
            color: 'red'
          },
          {
            value: '10006',
            label: '已支付',
            color: 'limegreen'
          },
          {
            value: '10007',
            label: '交易成功',
            color: 'limegreen'
          },
          {
            value: '10008',
            label: '已评价',
            color: 'limegreen'
          },
          {
            value: '10012',
            label: '工单发货',
            color: 'limegreen'
          },
          {
            value: '10013',
            label: '交易失败',
            color: 'red'
          },
          {
            value: '10009',
            label: '卖家超时取消',
            color: 'red'
          },
          {
            value: '10010',
            label: '买家超时取消',
            color: 'red'
          },
          {
            value: '10011',
            label: '超时自动完成',
            color: 'limegreen'
          },
          {
            value: '10015',
            label: '关闭出售',
            color: 'red'
          },
          {
            value: '10016',
            label: '买家取消交易',
            color: 'red'
          },
          {
            value: '10017',
            label: '仲裁中',
            color: 'gray'
          },
          {
            value: '10018',
            label: '已成交',
            color: 'limegreen'
          },
          {
            value: '10019',
            label: '卖家已评价',
            color: 'limegreen'
          },
        ],
        status: '',
        list: [],
        loading: false,
        finished: false,
        refreshing: false,

        pageSize: 20,
        currentPage: 1,

        transType: 1,
        tabValue: 0,
        countObj: {
          buyCount: {
            confirmCount: 0,
            payCount: 0,
            sendCount: 0,
            sum: 0,
          },
          saleCount: {
            confirmCount: 0,
            payCount: 0,
            sendCount: 0,
            sum: 0,
          }
        }
      }
    },
    computed: {
      ...mapState({
        baseData: state => state.global.baseData
      }),
    },
    methods: {
      async orderCount() {
        let {
          data
        } = await api.orderCount()
        if (data.code === 0) {
          this.countObj = data.data;
          this.countObj.buyCount.sum = this.countObj.buyCount.confirmCount + this.countObj.buyCount.payCount + this
            .countObj.buyCount.sendCount;
          this.countObj.saleCount.sum = this.countObj.saleCount.confirmCount + this.countObj.saleCount.payCount + this
            .countObj.saleCount.sendCount;
        }
      },
      onRefresh() {
        this.list = []
        this.currentPage = 1;
        this.finished = false;
        this.onLoad(true);
      },
      filterEmptyValues(obj) {
        return Object.fromEntries(
          Object.entries(obj).filter(([key, value]) =>
            value !== null && value !== undefined && value !== ''
          )
        );
      },
      onLoad(flag = false) {
        this.loading = true;
        let params = {
          transType: this.transType,
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          status: this.status,
        }
        if (this.transType === 1) {
          params = {
            ...params,
          }
        }
        params = this.filterEmptyValues(params)
        if (typeof that.source === "function") {
          that.source("终止请求"); //取消请求
        }
        api.orderList(params, that).then(res => {
          if (res.data.code == 0) {
            res.data.data.records.forEach(item => {
              item.receivePayment = new Array;
              item.receivePaymentType.split(',').forEach(xitem => {
                if (['3', '4', '5', '6', '7'].indexOf(xitem) !== -1) {
                  item.receivePayment.push(xitem)
                }
              })
            })
            if (flag) {
              this.list = res.data.data.records || [];
            } else {
              this.list = this.list.concat(res.data.data.records || []);
            }
            // 数据全部加载完成
            if (res.data.data.hasNext == false) {
              this.finished = true;
            } else {
              this.currentPage++;
            }
          } else {
            this.finished = true;
          }
          this.loading = false;
          this.refreshing = false;
        });
      },
      changeTransType(val) {
        this.transType = val;
        this.status = '';
        this.onRefresh()
      },
      changeStatus(val) {
        this.status = val;
        this.onRefresh()
      },
      getTradeType(val) {
        let obj = {}
        this.statusList.forEach(item => {
          if (val.toString() === item.value) {
            obj = item;
          }
        })
        return obj;
      },
      goEvaluation(val) {
        this.$toast("跳转评价页面");
      }
    },
    mounted() {
      this.orderCount()
      this.onLoad(true)
    },
  }
</script>

<style lang="scss" rel="stylesheet/scss" type="text/scss" scoped>
  .badge{
    position: absolute;
    background: red;
    color: #fff;
    text-align: center;
    line-height: 16px;
    width: 16px;
    height: 16px;
    background: #ff543a;
    border-radius: 9px;
    font-size: 10px;
    right: -10px;
    top: -7px;
  }
  .badge2{
    position: absolute;
    background: red;
    color: #fff;
    text-align: center;
    line-height: 16px;
    width: 16px;
    height: 16px;
    background: #ff543a;
    border-radius: 9px;
    font-size: 10px;
    right: -13px;
    top: -9px;
  }
  .contents{
    background: #F0F5FD;
    height: 100%;
    /deep/.van-tab{
      padding: 0;
      text-align: center;
      width: 20%;
      flex: auto;
    }
    /deep/.van-tabs__line{
      display: none;
    }
    .blueText{
      color: #479FFF;
    }
    .bigBtn{
      background: #fff;
      height: 75px;
      display: flex;
      align-items: center;
      justify-content: center;
      div{
        width: calc(100% - 54px);
        background: #479fff;
        border-radius: 4px;
        font-size: 14px;
        color: #fff;
        text-align: center;
        line-height: 44px;
      }
    }
    .popModel{
      .title1{
        line-height: 56px;
        text-align: center;
        border-bottom: 1px solid #EFF0F3;
        font-size: 16px;
        font-weight: Medium;
        color: #222222;
      }
      .c{
        padding: 20px 15px 0 15px;
      }
      .btns{
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 15px 20px 15px;
        .btn1{
          width: calc(50% - 5px);
          height: 44px;
          line-height: 44px;
          text-align: center;
          border: 1px solid #479fff;
          border-radius: 4px;
          color: #479FFF;
          font-size: 14px;
        }
        .btn2{
          width: calc(50% - 5px);
          height: 44px;
          line-height: 44px;
          text-align: center;
          background: #479fff;
          border-radius: 4px;
          color: #fff;
          font-size: 14px;
        }
      }
      .range{
        margin-bottom: 10px;
        .title{
          height: 20px;
          font-size: 14px;
          font-weight: Medium;
          color: #444444;
          line-height: 20px;
          margin-bottom: 8px;
        }
        .amount{
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-bottom: 10px;
          input{
            width: 45%;
            line-height: 40px;
            height: 40px;
            background: #f7f7f7;
            border-radius: 4px;
            font-size: 13px;
            border: none;
            outline: none;
            text-align: center;
          }
          .line{
            width: 5%;
            height: 1.5px;
            background: #eff0f3;
          }
        }
        .models{
          display: flex;
          flex-wrap: wrap;
          justify-content: space-between;
          .item{
            width: calc(33% - 5px);
            height: 36px;
            background: #f7f7f7;
            border-radius: 4px;
            font-size: 13px;
            font-weight: Regular;
            text-align: center;
            color: #444444;
            line-height: 36px;
            margin-bottom: 7.5px;
          }
          .checked{
            background: #ebf5ff;
            border: 1px solid #479fff;
            border-radius: 4px;
          }
        }
      }
    }
    .top{
      padding-top: 12px;
      display: flex;
      align-items: flex-end;
      justify-content: space-between;
      padding-bottom: 15px;
      .tabs{
        span{
          width: 68px;
          height: 24px;
          font-size: 17px;
          font-weight: Medium;
          text-align: left;
          color: #8f949c;
          line-height: 24px;
          margin: 15px;
          position: relative;
        }
        .checked{
          color: #222222;
          &::after{
            content: '';
            width: 40px;
            height: 5px;
            background: #479fff;
            display: block;
            position: absolute;
            bottom: -6px;
            left: 12px;
          }
        }
      }
    }
    .content{
      height: calc(100% - 115px);
      overflow-y: auto;
    }
    .card{
      padding: 15px;
      border-bottom: 1px solid #EFF0F3;
      background: #fff;
      position: relative;
      .info{
        display: flex;
        align-items: center;
        margin-bottom: 8px;
        .headImg{
          width: 20px;
          height: 20px;
          border-radius: 50%;
        }
        .creditImg{
          width: 38px;
          height: 14px;
        }
        span{
          height: 20px;
          font-size: 14px;
          font-weight: Regular;
          color: #444444;
          line-height: 20px;
          margin: 0 8px;
        }
      }
      .line1{
        display: flex;
        align-items: center;
        margin-bottom: 4px;
        .t1{
          height: 14px;
          font-size: 10px;
          font-weight: Regular;
          color: #8f949c;
          line-height: 14px;
          margin-right: 8px;
        }
        .t2{
          height: 17px;
          font-size: 17px;
          font-weight: Medium;
          color: #444444;
          line-height: 17px;
        }
        .t5{
          margin-right: 5px;
        }
      }
      .line2{
        height: 14px;
        font-size: 10px;
        font-weight: Regular;
        margin-bottom: 5px;
        .t1{
          color: #8f949c;
          margin-right: 8px;
        }
        .t2{
          color: #444444;
        }
      }
      .line3{
        display: flex;
        align-items: center;
        img{
          width: 14px;
          height: 14px;
          margin-right: 8px;
        }
      }
      .right{
        position: absolute;
        right: 15px;
        top: 18px;
        display: flex;
        flex-direction: column;
        align-items: flex-end;
        .hide{
          opacity: 0;
        }
        .line1{
          height: 14px;
          font-size: 10px;
          font-weight: Regular;
          color: #8f949c;
          line-height: 14px;
          margin-bottom: 8px;
        }
        .line2{
          display: flex;
          align-items: center;
          margin-bottom: 15px;
          .t1{
            height: 17px;
            font-size: 17px;
            font-weight: Medium;
            color: #444444;
            line-height: 17px;
            margin-right: 8px;
          }
          .t2{
            height: 14px;
            font-size: 10px;
            font-weight: Regular;
            color: #8f949c;
            line-height: 14px;
          }
        }
        .flex{
          display: flex;
          align-items: center;
          .redT{
            color: red;
            font-size: 18px;
          }
          .greenT{
            color: limegreen;
            font-size: 18px;
          }
        }
        .btn{
          width: 60px;
          text-align: center;
          color: #fff;
          line-height: 27px;
          height: 27px;
          background: #479fff;
          border-radius: 4px;
          font-size: 13px;
          margin-left: 10px;
        }
        .btn2{
          width: 60px;
          text-align: center;
          color: #479fff;
          border: 1px solid #479fff;
          line-height: 27px;
          height: 27px;
          background: #fff;
          border-radius: 4px;
          font-size: 13px;
          margin-left: 10px;
        }
      }
    }
  }
  .orderPopClass{
    padding: 10px;
    /deep/.van-dialog__footer{
      padding: 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    /deep/.van-dialog__cancel{
      width: calc(50% - 8px);
      height: 44px;
      line-height: 44px;
      font-size: 15px;
      text-align: center;
      border: 1px solid #479fff;
      border-radius: 4px;
      background-color: #fff;
      color: #479FFF;
      flex: none;
    }
    /deep/.van-dialog__confirm{
      width: calc(50% - 8px);
      height: 44px;
      line-height: 44px;
      font-size: 15px;
      text-align: center;
      background: #479fff;
      border-radius: 4px;
      color: #fff;
      flex: none;
    }
    /deep/.van-dialog__content{
      line-height: 70px;
      text-align: center;
    }
  }
</style>
